package paritclecontainer2d;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class Emiter {

	//pseudo thread safe arraylist
	protected CopyOnWriteArrayList<Particle> particles;
	
	public Emiter(){
		this.particles = new CopyOnWriteArrayList<>();
	}
	
	public CopyOnWriteArrayList<Particle> getParticles() {
		return particles;
	}

	public void spawnParticle(Particle p, int amount, int x, int y) {
		for(int i = 0; i < amount; i++){
			Particle particle;
			try {
				particle = (Particle) p.clone();
				particle.setX(x);
				particle.setY(y);
				particles.add(particle);
				
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}

		}
	}
	
	public void refreshParticles(){
		for(Particle particle : particles){
			emit(particle);
			decay(particle);
		}
	}
	
	protected void kill(Particle particle) {
		particles.remove(particle);
	}
	
	protected abstract void decay(Particle particle);
	protected abstract void emit(Particle particle);
}
